﻿using EquipmentManager.Entities;
using EquipmentManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EquipmentManager.BOLogic
{
    public class UserBO : BOBase
    {
        public List<User> GetUserList()
        {
            try
            {
                var users = db.Set<User>().Where(u => u.Status == EntityStatus.Active).OrderBy(u => u.RegistredDate).ToList();

                return users;
            }
            catch (Exception ex)
            {
                return new List<User>();
            }
        }

        public List<User> GetRecentUserList()
        {
            try
            {
                DateTime weekAgo = DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));

                var users = db.Set<User>().Where(u => u.Status == EntityStatus.Active && 
                    u.RegistredDate >= weekAgo).OrderBy(u => u.RegistredDate).ToList();

                return users;
            }
            catch (Exception ex)
            {
                return new List<User>();
            }
        }

        public User GetUser(long UserId)
        {
            try
            {
                var user = db.Set<User>().Single(u => u.Id == UserId);

                return user;
            }
            catch (Exception ex)
            {
                return new User();
            }
        }

        public User GetCurrentUser()
        {
            try
            {
                var user = db.Set<User>().Single(u => u.Id == User.Id);

                return user;
            }
            catch (Exception ex)
            {
                return new User();
            }
        }

        public bool IsLoginUnique(CreateModel<User> model)
        {
            var isExist = db.Set<User>().Where(u => u.UserName == model.Instance.UserName).ToList();

            if (isExist.Count == 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public bool CreateNewUser(CreateModel<User> model)
        {
            try
            {
                var admin = db.Set<User>().Single(u => u.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    model.Instance.RegistredDate = DateTime.Now;
                    model.Instance.Region = db.Set<Region>().Single(r => r.Id == model.ParentId);

                    if (model.Instance.Password != String.Empty && model.Instance.Password != null)
                    {
                        CryptoBO cryptedPassword = new CryptoBO(model.Instance.Password.ToCharArray());
                        model.Instance.Password = cryptedPassword.HashedPassword;
                    }
                    else
                    {
                        return false;
                    }

                    db.Set<User>().Add(model.Instance);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool EditUser(CreateModel<User> model)
        {
            try
            {
                var user = db.Set<User>().Single(c => c.Id == model.Instance.Id);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin)
                {
                    // можно создавать новую измененную, а эту делать неактивной если что;)
                    user.UserName = model.Instance.UserName;
                    user.FullName = model.Instance.FullName;

                    if (model.Instance.Password != String.Empty && model.Instance.Password != null)
                    {
                        CryptoBO cryptedPassword = new CryptoBO(model.Instance.Password.ToCharArray());
                        user.Password = cryptedPassword.HashedPassword;
                    }

                    user.Role = model.Instance.Role;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool EditUserProfile(CreateModel<User> model)
        {
            try
            {
                var user = db.Set<User>().Single(c => c.Id == model.Instance.Id);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (user.Id == User.Id)
                {
                    // можно создавать новую измененную, а эту делать неактивной если что;)
                    user.UserName = model.Instance.UserName;
                    user.FullName = model.Instance.FullName;

                    if (model.Instance.Password != String.Empty && model.Instance.Password != null)
                    {
                        CryptoBO cryptedPassword = new CryptoBO(model.Instance.Password.ToCharArray());
                        user.Password = cryptedPassword.HashedPassword;
                    }

                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteUser(long UserId)
        {
            try
            {
                var user = db.Set<User>().Single(u => u.Id == UserId);
                var admin = db.Set<User>().Single(c => c.Id == User.Id);

                if (admin.Role == UserRole.Admin && user.Id != admin.Id)
                {
                    user.Status = EntityStatus.Deleted;
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    throw new Exception("Attempt of unauthorized access.");
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}